package com.foreveross.proxyip.infra.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

public class MapUtils {

	/**
	 * 从Map中获取指定键对应的字符串类型的值
	 * @author zhouan, 2012-7-21
	 * @param params
	 * @param key
	 * @return
	 */
	public static String getParameterFromMap(Map params, String key, String defaultStr) {
		Object obj = params.get(key);
		if (obj instanceof Object[]) {
			Object[] values = (Object[]) obj;
			if (!values[0].toString().equals("")) 
				return values[0].toString();
		}else if (obj instanceof Object) {
			return obj.toString();
		}
		return defaultStr;
	}
	
	/**
	 * 从Map中获取指定键对应的字符串数组
	 * @author zhouan, 2012-7-21
	 * @param params
	 * @param key
	 * @return
	 */
	public static String[] getParametersFromMap(Map params, String key) {
		Object obj = params.get(key);
		if (obj instanceof String[]) {
			String[] values = (String[]) obj;
			return values;
		} else if (obj instanceof Object[]) {
			Object[] objs = (Object[]) obj;
			String[] values = new String[objs.length];
			for (int i = 0; i < values.length; i++) {
				values[i] = objs[i].toString();
			}
			return values;
		}else if (obj instanceof Object) {
			String[] values = new String[]{obj.toString()};
			return values;
		}
		return new String[0];
	}
	
	/**
	 * 从Map中获取指定键对应的int型数值
	 * @author zhouan, 2012-7-21
	 * @param params
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static int getIntParameterFromMap(Map params, String key, int defaultValue) {
		Object obj = params.get(key);
		if (obj instanceof Object[]) {
			Object[] values = (Object[]) obj;
			return Integer.parseInt(values[0].toString());
		}else if (obj instanceof Object) {
			return (Integer)obj;
		}
		return defaultValue;
	}
	
	/**
	 * 从Map中获取指定键对应的int型数组
	 * @author zhouan, 2012-7-21
	 * @param params
	 * @param key
	 * @return
	 */
	public static int[] getIntParametersFromMap(Map params, String key) {
		Object obj = params.get(key);
		if (obj instanceof int[]) {
			int[] values = (int[]) obj;
			return values;
		} else if (obj instanceof Object[]) {
			Object[] objs = (Object[]) obj;
			int[] values = new int[objs.length];
			for (int i = 0; i < values.length; i++) {
				values[i] = Integer.parseInt(objs[i].toString());
			}
			return values;
		}else if (obj instanceof Object) {
			int[] values = new int[]{(Integer)obj};
			return values;
		}
		return new int[0];
	}
	
	/**
	 * 从Map中获取指定键对应的Integer类型型数值
	 * @author zhouan, 2012-7-21
	 * @param params
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Integer getIntegerParameterFromMap(Map params, String key, Integer defaultValue) {
		Object obj = params.get(key);
		if (obj instanceof Object[]) {
			Object[] values = (Object[]) obj;
			if (!values[0].toString().equals(""))
				return Integer.parseInt(values[0].toString());
		}else if (obj instanceof Object) {
			if (!obj.toString().equals(""))
				return (Integer)obj;
		}
		return defaultValue;
	}
	
	/**
	 * 工具类:将map中的key和值设定到request中,以map的key为AttributeuteName,以map的value为AttributeuteValue
	 * @param startMapKey Map中key名称的起始字符串
	 * @param map map数据
	 * @param request web HttpServletRequest对象
	 * @param startAttributeName 设定request中值AttributeName的起始字符串
	 */
	public static void setAttributeuteStartingWith(String startMapKey, Map map, HttpServletRequest request, String startAttributeName) {
		Set tempSet = map.entrySet();
		String key = "";
		Iterator i = tempSet.iterator();
		while (i.hasNext()) {
			Entry e = (Entry) i.next();
			if (null != startMapKey) {
				key = e.getKey().toString();
				if (key.indexOf(startMapKey) >= 0) {
					if (null != startAttributeName)
						request.setAttribute(startAttributeName + key, e.getValue());
					else
						request.setAttribute(key, e.getValue());
				}
			}
		}
	}

	/**
	 * 工具类:将request中的以startName开始的Parameters设定到request的Attribute中
	 * @param request web HttpServletRequest对象
	 * @param startName request中值ParametersName的起始字符串
	 */
	public static void setAttributeuteStartingWithParameters(HttpServletRequest request, String startName) {
		Map map = request.getParameterMap();
		Set tempSet = map.entrySet();
		String key = "";
		Iterator i = tempSet.iterator();
		while (i.hasNext()) {
			Entry e = (Entry) i.next();
			if (null != startName) {
				key = e.getKey().toString();
				if (key.indexOf(startName) >= 0) {
					request.setAttribute(key, e.getValue());
				}
			}
		}

	}

	/**
	 * 工具类:将request中的以startName开始的Parameters删除
	 * @param request web HttpServletRequest对象
	 * @param startName request中值ParametersName的起始字符串
	 */
	public static void delAttributeuteStartingWithParameters(HttpServletRequest request, String startName) {
		Map map = request.getParameterMap();
		Set tempSet = map.entrySet();
		String key = "";
		Iterator i = tempSet.iterator();
		while (i.hasNext()) {
			Entry e = (Entry) i.next();
			if (null != startName) {
				key = e.getKey().toString();
				if (key.indexOf(startName) >= 0) {
					request.removeAttribute(key);
				}
			}
		}

	}
}